package com.appkubes.mall.restful.application.goods.rent.controller;

import com.appkubes.base.core.entity.vo.AppKeyVO;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.core.entity.MallGoodsCategory;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.goods.category.service.CategoryService;
import com.appkubes.mall.restful.application.goods.goods.entity.dto.MallCategoryRentDTO;
import com.appkubes.mall.restful.application.goods.goods.entity.dto.MallGoodsRentDTO;
import com.appkubes.mall.restful.application.goods.goods.entity.vo.MallCategoryVo;
import com.appkubes.mall.restful.application.goods.rent.service.GoodsRentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api("获取租赁商品模块")
@RestController
@RequestMapping(value = "/good/rent")
public class GoodsRentController extends BaseController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    GoodsRentService goodsRentService;

    @ApiOperation(value = "获取首页模块列表")
    @PostMapping("/getGoodsOrderByClassify")
    public Response getGoodsOrderByClassify(@RequestBody @ParamValid AppKeyVO appKeyVO) {

        List<MallGoodsCategory> mallGoodsCategoryList = categoryService.getGoodsCategory(appKeyVO, 2);

        List<MallCategoryRentDTO> mallCategoryDtoList = new ArrayList<>();
        for (MallGoodsCategory mallGoodsCategory : mallGoodsCategoryList) {
            MallCategoryRentDTO mallCategoryDto = new MallCategoryRentDTO();
            BeanUtils.copyProperties(mallGoodsCategory, mallCategoryDto);
            MallCategoryVo mallCategoryVo = new MallCategoryVo();
            mallCategoryVo.setAppKey(appKeyVO.getAppKey());
            mallCategoryVo.setCategoryKey(mallGoodsCategory.getCategoryKey());
            mallCategoryDto.setGoods(goodsRentService.selectGoods(mallCategoryVo));
            mallCategoryDtoList.add(mallCategoryDto);
        }

        return imgResponse(mallCategoryDtoList);
    }

    @ApiOperation("获取租赁商城分类商品")
    @PostMapping("/getCategoryGoods")
    public Response getCategoryGoods(@RequestBody @Valid MallCategoryVo mallCategoryVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.error("参数检查失败");
        }
        PageHelper.startPage(mallCategoryVo.getPage(), mallCategoryVo.getSize());
        List<MallGoodsRentDTO> mallGoodsRentDTOList = goodsRentService.selectGoods(mallCategoryVo);
        PageInfo<MallGoodsRentDTO> pageInfo = new PageInfo<>(mallGoodsRentDTOList);
        return imgResponse(pageInfo);
    }
}
